package com.chao.wind3j.base.config.commonConfig.requestLogConfig.requestWrapper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chao.wind3j.base.config.commonConfig.CommonProperties;

import com.chao.wind3j.util.DateFormat;
import com.chao.wind3j.util.DateUtil;
import com.chao.wind3j.util.IpUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.*;

public class CustomRequestWrapper extends HttpServletRequestWrapper {

  private RequestInfo requestInfo;

  private final CommonProperties commonProperties;

  public CustomRequestWrapper(HttpServletRequest request, CommonProperties commonProperties) {
    super(request);
    this.commonProperties = commonProperties;
    this.castRequestToRequestInfo(request);
  }

  public void castRequestToRequestInfo(HttpServletRequest request) {
    this.requestInfo = new RequestInfo();
    this.requestInfo.setIp(IpUtil.getRequestIp(request));
    this.requestInfo.setPath(request.getRequestURI());
    this.requestInfo.setRequestMethod(request.getMethod());
    this.requestInfo.setContentType(request.getContentType());
    this.requestInfo.setRemoteUser(request.getRemoteUser());
    this.requestInfo.setAuthToken(this.getAuthToken(request));
    this.requestInfo.setRequestTime(DateUtil.getFormatDate(new Date(), DateFormat.format_yyyy_MM_dd_HH_mm_ss));
    this.requestInfo.setRequestHeaders(getRequestHeaders(request));
    this.requestInfo.setRequestFrontUrlParams(getRequestFrontUrlParamsArray(request));
  }

  /**
   * 获取header参数
   * @param request
   * @return
   */
  private Map<String, Object> getRequestHeaders(HttpServletRequest request) {
    var headers = new HashMap<String, Object>();
    var headersItems = request.getHeaderNames();
    while(headersItems.hasMoreElements()) {
      var headersKey = headersItems.nextElement();
      headers.put(headersKey, request.getHeader(headersKey));
    }
    return headers;
  }

  /**
   * 获取前端传过来的在url的参数
   * @param request
   * @return
   */
  private Map<String, Object> getRequestFrontUrlParamsArray(HttpServletRequest request) {
    var params = new JSONObject();
    // 首先取得在url后面追加的参数
    var paramsMap = request.getParameterMap();
    if (paramsMap == null || paramsMap.size() == 0){
      return null;
    }
    for(var item : paramsMap.entrySet()) {
      var key = item.getKey();
      var values = item.getValue();
      if(values == null) {
        params.put(key, null);
      } else if(values.length == 1){
        params.put(key, values[0]);
      } else {
        params.put(key, values);
      }
    }

    return params;
  }

  /**
   * 获取token信息
   * @param request
   * @return
   */
  private AuthTokenVo getAuthToken(HttpServletRequest request) {
    var authTokenVo = new AuthTokenVo();
    var authToken = request.getHeader(this.commonProperties .getAuthToken());
    if (authToken != null) {
      var authTokenJson = (JSONObject) JSON.parse(authToken);
      authTokenVo.setToken(authTokenJson.getString("token"));
    }
    return authTokenVo;
  }

  public RequestInfo getRequestInfo() {
    return requestInfo;
  }
}
